var file = {

    files : [],
    table : null,
    dirPath : null,
    sortStr : "desc",

    load : function(path) {

        // 赋予路径
        if (path) {
            this.dirPath = path;
        } else {
            this.dirPath = config.dirPath;
        }
        if (!this.dirPath) {
            console.log("没有默认路径，使用当前目录");
            this.dirPath = "D:/";
            // 写入到config中
        }
        if (this.dirPath.lastIndexOf("/") != this.dirPath.length - 1) {
            this.dirPath += "/";
        }
        console.log("fileDirPath：" + this.dirPath);
        // 清空
        $("#fileTableDiv").html("");
        this.table = null;

        // 加载文件
        var fileNames = fs.readdirSync(this.dirPath);
        this.files = [];
        for (var i = 0; i < fileNames.length; i++) {
            var file = {
                oldName : fileNames[i],
                newName : null
            };
            this.files.push(file);
        }

        this.sort();
    },

    createHtml : function() {

        $("#fileTableDiv").html("");
        this.table = null;
        // 创建新的
        this.table = $('<table id="fileTable" class="fileTable" cellspacing="0" cellpadding="0"></table>');
        this.table.html('<tr><td class="fileNameTitle">原始文件名</td><td class="fileNameTitle">新文件名</td></tr>');
        $("#fileTableDiv").append(this.table);

        for (var i = 0; i < this.files.length; i++) {
            var file = this.files[i];
            this._createRow(file);
        }

    },

    // 创建单个的
    _createRow : function(file) {
        var td1 = $("<td class='oldFileName'></td>").html(file.oldName);
        var td2 = $("<td class='newFileName'></td>").html(file.newName);
        var tr = $("<tr></tr>").append(td1).append(td2).appendTo(this.table);

        file.oldNameTd = td1;
        file.newNameTd = td2;
        file.tr = tr;

        if (file.oldName != file.newName) {
            file.newNameTd.addClass("oldFileNameChange");
        } else {
            file.newNameTd.removeClass("oldFileNameChange");
        }

        tr.mouseover(function() {
                    $(this).find("td:first").css("background-color", "#00AAFF");
                    $(this).find("td:last").css("background-color", "#FFAA00");
                }).mouseout(function() {
                    $(this).find("td").css("background-color", "#FFFFFF");
                });
    },

    rename : function() {
        for (var i = 0; i < this.files.length; i++) {
            var file = this.files[i];
            var oldName = file.oldName;
            var newName = file.newName;
            if (newName && oldName != newName) {
                fs.rename(this.dirPath + oldName, this.dirPath + newName, function() {
                            file.isRenameOk = true;
                        });
            }
        }
        this.load(this.dirPath);
        this.createHtml();
    },

    // 大写
    toUpperCase : function() {
        for (var i = 0; i < this.files.length; i++) {
            var obj = this.files[i];
            var fileName = obj.oldName;
            var p = fileName.lastIndexOf(".");
            if (p > 0) {
                fileName = fileName.substring(0, p);
                fileName = fileName.toLocaleUpperCase();
                var newName = fileName + obj.oldName.substring(p);
                obj.newName = newName;
            } else {
                obj.newName = fileName;
            }
        }
        this.createHtml();
    },
    // 小写
    toLowerCase : function() {
        for (var i = 0; i < this.files.length; i++) {
            var obj = this.files[i];
            var fileName = obj.oldName;
            var p = fileName.lastIndexOf(".");
            if (p > 0) {
                fileName = fileName.substring(0, p);
                fileName = fileName.toLocaleLowerCase();
                var newName = fileName + obj.oldName.substring(p);
                obj.newName = newName;
            } else {
                obj.newName = fileName;
            }

        }
        this.createHtml();
    },

    // 扩展名小写
    extToLowerCase : function() {
        for (var i = 0; i < this.files.length; i++) {
            var obj = this.files[i];
            var fileName = obj.oldName;
            var p = fileName.lastIndexOf(".");
            if (p > 0) {
                var baseName = fileName.substring(0, p)
                var extName = fileName.substring(p + 1);
                extName = extName.toLocaleLowerCase();
                var newName = baseName + "." + extName;
                obj.newName = newName;
            } else {
                obj.newName = fileName;
            }

        }
        this.createHtml();
    },
    // 扩展名大写
    extToUpperCase : function() {
        for (var i = 0; i < this.files.length; i++) {
            var obj = this.files[i];
            var fileName = obj.oldName;
            var p = fileName.lastIndexOf(".");
            if (p > 0) {
                var baseName = fileName.substring(0, p)
                var extName = fileName.substring(p + 1);
                extName = extName.toLocaleUpperCase();
                var newName = baseName + "." + extName;
                obj.newName = newName;
            } else {
                obj.newName = fileName;
            }

        }
        this.createHtml();
    },
    // 优酷文件重命名
    youkuRename : function() {
        for (var i = 0; i < this.files.length; i++) {
            var obj = this.files[i];
            var fileName = obj.oldName;
            var p = fileName.lastIndexOf(".");
            var baseName = fileName;
            var extName = "";

            if (p > 0) {
                baseName = fileName.substring(0, p)
                extName = fileName.substring(p + 1);
            }

            var sp = baseName.lastIndexOf(" ");
            if (sp > 0) {
                var first = baseName.substring(0, sp);
                var second = baseName.substring(sp + 1);
                var newName = second + "_" + first;
                obj.newName = newName + "." + extName;
            } else {
                obj.newName = fileName;
            }
        }
        this.createHtml();
    },

    sort : function() {
        this.files.sort(function(a, b) {
                    if (this.sortStr == "asc") {
                        return a.localeCompare(b);
                    }
                    if (this.sortStr == "desc") {
                        return b.localeCompare(a);
                    }
                });
    }

}
